package com.example.remindme.RemindModule.DataCard.Fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.remindme.R
import com.example.remindme.Tag
import com.example.remindme.RemindModule.DataCard.ViewHolder.ClickData
import com.example.remindme.RemindModule.DataCard.ViewHolder.MyList
import com.example.remindme.RemindModule.DataCard.ViewHolder.RecyclerData
import com.example.remindme.RemindModule.DataCard.ViewHolder.ViewAdapter
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class ViewcardFragment() : Fragment() {

    lateinit  var viewModel: ViewcardViewModel
    lateinit var todayCard:CardView
    lateinit var flagCard:CardView
    lateinit var totalCard:CardView
    lateinit var scheduledCardView: CardView
    lateinit var todayCons:ConstraintLayout
    lateinit var flagCons:ConstraintLayout
    lateinit var totalCons:ConstraintLayout
    lateinit var schedule_cons:ConstraintLayout
    lateinit var recyclerView: RecyclerView

    lateinit var todaynum:TextView
    lateinit var allnum:TextView
    lateinit var scheduledNum:TextView
    lateinit var flagNum:TextView

    var landRecycler: RecyclerView? =null

    var data= arrayListOf<RecyclerData>()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.viewcard_fragment, container, false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(Tag, "viewCardFragment created")
        super.onCreate(savedInstanceState)
    }

    override fun onDestroy() {
        Log.d(Tag, "viewCardFragment destroy")
        super.onDestroy()
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val v=view
        if(v!=null)
        {
            todayCard=v.findViewById(R.id.todaycard)
            flagCard=v.findViewById(R.id.flagcard)
            totalCard=v.findViewById(R.id.totalcard)
            scheduledCardView=v.findViewById(R.id.schedulecard)
            schedule_cons=v.findViewById<ConstraintLayout>(R.id.constains_chedule)
            todayCons=v.findViewById(R.id.constrain_today)
            flagCons=v.findViewById(R.id.constrain_flag)
            totalCons=v.findViewById(R.id.constaintotal)
            recyclerView=v.findViewById(R.id.cardRecycler)

            todaynum=v.findViewById(R.id.todaynum)
            allnum=v.findViewById(R.id.totalNum)
            scheduledNum=v.findViewById(R.id.scheduleNum)
            flagNum=v.findViewById(R.id.flagNum)

        }
       viewModel.current= ClickData(todayCons, false)
       initClickAction()
        initDBObserver()

        GlobalScope.launch {
         viewModel.requeryRoom()
        }
    }

    fun initDBObserver()
    {
        viewModel.matterDao= viewModel.matterDatabase.matterDao()
        viewModel.listDao=viewModel.matterDatabase.listDao()  // 加载不同的包
      //  viewModel.matterDao.AllMatters
        activity?.let {
            viewModel.finishRoom.observe(it, {
                // 完成再赋值
                data = viewModel.listNameData
                todaynum.text=viewModel.todayNum.toString()
                allnum.text=viewModel.allNum.toString()
                scheduledNum.text=viewModel.scheduledNum.toString()
                flagNum.text=viewModel.flagNum.toString()

                initViewHolder()
            })
        }
    }

    private fun initViewHolder() {
        // 设置recy的布局
        recyclerView.layoutManager= LinearLayoutManager(activity)
        val LandAdapter = ViewAdapter(viewModel.listMatterData)
        val adapter= ViewAdapter(data)  // adapeter 会在内部完成
        recyclerView.adapter=adapter
        recyclerView.setHasFixedSize(true)
        if(landRecycler!=null)
        {
            landRecycler!!.layoutManager=LinearLayoutManager(activity)
            landRecycler!!.adapter=LandAdapter
            landRecycler!!.setHasFixedSize(true)
        }
// 匿名对象
        adapter.setOnItemClick(object : ViewAdapter.onRecyclerViewItemClickListener {
            // 对按钮设置点击响应事件
            override fun onItemClick(view: View, position: Int) {
                if (viewModel.current != null)
                    viewModel.current.layout.setBackgroundColor(resources.getColor(R.color.white))
                val linecons = view.findViewById<ConstraintLayout>(R.id.line_cons)
                val blockcons = view.findViewById<ConstraintLayout>(R.id.blockCons)
                val listcons = view.findViewById<ConstraintLayout>(R.id.listCons)
                if (linecons != null) {
                    linecons.setBackgroundColor(resources.getColor(R.color.click))
                    viewModel.current = ClickData(linecons, true)
                } else if (blockcons != null) {
                    blockcons.setBackgroundColor(resources.getColor(R.color.click))
                    viewModel.current = ClickData(blockcons, true)
                } else if (listcons != null) {
                    // 说明此时选择的是一个list
                    listcons.setBackgroundColor(resources.getColor(R.color.click))
                    viewModel.current = ClickData(listcons, true)
                    if (data[position] is MyList) {
                        // 点击到了某个列表，将这个列表设置为livadata类型，方便再main里面调用
                        viewModel.currentType.value = (data[position] as MyList).listType
                    }
                }

            }
        })


    }

    fun initClickAction() {
        // 对不同的按件设置响应
        todayCard.setOnClickListener{
            if(viewModel.current.clicked) // 说明此时是有类型的
            {
                viewModel.current.layout.setBackgroundColor(resources.getColor(R.color.white))
            }

            viewModel.currentType.value= TYPE_TODAY
          viewModel.current= ClickData(todayCons, true)
            todayCons.setBackgroundColor(resources.getColor(R.color.today_cardView_back))
        }
        totalCard.setOnClickListener{
            if(viewModel.current.clicked) // 说明此时是有类型的
            {
                viewModel.current.layout.setBackgroundColor(resources.getColor(R.color.white))
            }
            viewModel.currentType.value= TYPE_ALL
            Log.d("search", TYPE_ALL)
            viewModel.current= ClickData(totalCons, true)
            totalCons.setBackgroundColor(resources.getColor(R.color.allCardview_back))
        }
        flagCard.setOnClickListener{
            if(viewModel.current.clicked) // 说明此时是有类型的
            {
                viewModel.current.layout.setBackgroundColor(resources.getColor(R.color.white))
            }
            viewModel.currentType.value= TYPE_FALGGED
            viewModel.current= ClickData(flagCons, true)
            flagCons.setBackgroundColor(resources.getColor(R.color.flagCardView_back))
        }
        scheduledCardView.setOnClickListener{
            if(viewModel.current.clicked) // 说明此时是有类型的
            {
                viewModel.current.layout.setBackgroundColor(resources.getColor(R.color.white))
            }
            viewModel.currentType.value= TYPE_SCHEDULED
            viewModel.current= ClickData(schedule_cons, true)
            schedule_cons.setBackgroundColor(resources.getColor(R.color.scheduled_cardView_back))
        }
    }
}


